var APPRAISER_ID="update_role_1";
var REINSPECTOR_ID="update_role_2";
var APPRAISER_ROLE_ID="1";
var REINSPECTOR_ROLE_ID="2";
$(document).ready(function() {
	update_hideAllCompanySelectedMenu();
	update_initCompanyRadioButton();
	initUpdateUserDropDownMenu();
	
	checkBoxCheck('#update_role_1','#update_role_2','#update_role_3','#update_role_4','#update_role_5','#update_role_6');
	initRoleCheckBox();
});

function checkBoxCheck(id1,id2,id3,id4,id5,id6){
	if($(id1).attr("checked")){
		$(id2).attr("disabled",true);
		$(id3).attr("disabled",true);
		$(id4).attr("disabled",true);
		$(id5).attr("disabled",true);
		$(id6).attr("disabled",true);
	}else{
		$(id2).attr("disabled",false);
		$(id3).attr("disabled",false);
		$(id4).attr("disabled",false);
		$(id5).attr("disabled",false);
		$(id6).attr("disabled",false);
	}
	if($(id2).attr("checked")||$(id3).attr("checked")||$(id4).attr("checked")||$(id5).attr("checked")||$(id6).attr("checked")){
	
		$(id1).attr("disabled",true);	
	}else{
	
		$(id1).attr("disabled",false);	
	}
}

function initRoleCheckBox(){

	var radioValue = $('input[name=companyRadio_update][checked]').val();

	if(radioValue==1){
		
		//diabled role 1 role 2
		$("#update_role_1").attr("disabled",true);
		//alert('disable role_3');
		$("#update_role_2").attr("disabled",true);
		
	}else if(radioValue==2){
		//diabled role 1 
		$("#update_role_1").attr("disabled",true);
		
	}else if(radioValue==3){
		//diabled role 1 
		$("#update_role_1").attr("disabled",true);
	}
	
}

function initUpdateUserDropDownMenu(){
	//disabled menu by login user level
	var menuLayOut=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOut);	
	var userCompanyLevel=menuObj.userCompanyLevel;	
	
	if(userCompanyLevel==HOME_OFFICE_LEVEL){
		update_hideAllCompanySelectedMenu();
	}else if(userCompanyLevel==PROVINCIAL_LEVEL){
		disableSelectMenu("#provincial_user_update");
	}else if(userCompanyLevel==MUNIPITAL_LEVEL){
		disableSelectMenu("#provincial_user_update");
		disableSelectMenu("#municipal_user_update");
		
	}else if(userCompanyLevel=CLAIM_CENTER_LEVEL){
		disableSelectMenu("#provincial_user_update");
		disableSelectMenu("#municipal_user_update");
		disableSelectMenu("#center_user_update");
	}
	//control to show menu by user selected
	var needToUpdateUserCompanylevel=$("#needToUpdateUserCompanylevel").val();
	
	if(needToUpdateUserCompanylevel==HOME_OFFICE_LEVEL){	
	}else if(needToUpdateUserCompanylevel==PROVINCIAL_LEVEL){
		$("#provincial_user_update").css("display","");		
	}else if(needToUpdateUserCompanylevel==MUNIPITAL_LEVEL){
		$("#provincial_user_update").css("display","");
		$("#municipal_user_update").css("display","");			
	}else if(needToUpdateUserCompanylevel=CLAIM_CENTER_LEVEL){
		$("#provincial_user_update").css("display","");
		$("#municipal_user_update").css("display","");
		$("#center_user_update").css("display","");	
	}
}

function update_hideAllCompanySelectedMenu(){		
		$("#provincial_user_update").css("display","none");
		$("#municipal_user_update").css("display","none");
		$("#center_user_update").css("display","none");	
}



function update_initCompanyRadioButton(){
	var menuLayOut=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOut);
	var userCompanyLevel=menuObj.userCompanyLevel;		
	if(userCompanyLevel==HOME_OFFICE_LEVEL){				
		disableSelectMenu("#homeOfficeRadioButton_update");
	}else if(userCompanyLevel==PROVINCIAL_LEVEL){
		disableSelectMenu("#homeOfficeRadioButton_update");
	}else if(userCompanyLevel==MUNIPITAL_LEVEL){
		disableSelectMenu("#homeOfficeRadioButton_update");
		disableSelectMenu("#provincialRadioButton_update");		
	}else if(userCompanyLevel==CLAIM_CENTER_LEVEL){
		disableSelectMenu("#homeOfficeRadioButton_update");
		disableSelectMenu("#provincialRadioButton_update");	
		disableSelectMenu("#municipalRadioButton_update");		
	}	
}

function update_showProvincialSelectedMenu(){
	$("#provincial_user_update").css("display","");	
	$("#municipal_user_update").css("display","none");
	$("#center_user_update").css("display","none");
}

function update_showMunicipalSelectedMenu(){
	$("#provincial_user_update").css("display","");
	$("#municipal_user_update").css("display","");	
	$("#center_user_update").css("display","none");
}

function update_showCenterSelectedMenu(){
	$("#provincial_user_update").css("display","");
	$("#municipal_user_update").css("display","");
	$("#center_user_update").css("display","");
}

function update_initRoleCheckBoxButton(){
	var companyRadioButtonValue=$('input[name=companyRadio_update][checked]').val();
	if(companyRadioButtonValue==HOME_OFFICE_LEVEL){	
		$("#update_role_1").attr("disabled",true);
		$("#update_role_2").attr("disabled",true);
		$("#rein_level_update").css("display","none");
		$("#update_role_3").attr("disabled",false);
		$("#update_role_4").attr("disabled",false);
		$("#update_role_5").attr("disabled",false);
		$("#update_role_6").attr("disabled",false);
	}else if(companyRadioButtonValue==PROVINCIAL_LEVEL){
		$("#update_role_1").attr("disabled",true);	
		$("#update_role_2").attr("disabled",false);
		$("#update_role_3").attr("disabled",false);
		$("#update_role_4").attr("disabled",false);
		$("#update_role_5").attr("disabled",false);
		$("#update_role_6").attr("disabled",false);
		update_showRevelTr();
	}else if(companyRadioButtonValue==MUNIPITAL_LEVEL){
		$("#update_role_1").attr("disabled",true);	
		$("#update_role_2").attr("disabled",false);
		$("#update_role_3").attr("disabled",false);
		$("#update_role_4").attr("disabled",false);
		$("#update_role_5").attr("disabled",false);
		$("#update_role_6").attr("disabled",false);
		update_showRevelTr();
		
	}else{
	
		
		if($("#update_role_2").attr("checked")||$("#update_role_3").attr("checked")||$("#update_role_4").attr("checked")||$("#update_role_5").attr("checked")||$("#update_role_6").attr("checked")){
			$("#update_role_1").attr("disabled",true);
			$("#update_role_2").attr("disabled",false);
			$("#update_role_3").attr("disabled",false);
			$("#update_role_4").attr("disabled",false);
			$("#update_role_5").attr("disabled",false);
			$("#update_role_6").attr("disabled",false);
		}else{		
			$("#update_role_1").attr("disabled",false);	
			$("#update_role_2").attr("disabled",false);
			$("#update_role_3").attr("disabled",false);
			$("#update_role_4").attr("disabled",false);
			$("#update_role_5").attr("disabled",false);
			$("#update_role_6").attr("disabled",false);
		}
		update_showRevelTr();
	}
}
function update_showRevelTr(){
	
		if($("#"+REINSPECTOR_ID).attr("checked")){
			$("#rein_level_update").css("display","");
		}else{
			$("#rein_level_update").css("display","none");
		}
	
}

function getMunicipalUpdateUser(){
	
	 var selectedProvincialCompany=$("#provincial_user_update").val();
	 if(selectedProvincialCompany==0||selectedProvincialCompany==-1){
	   	clearDropDownSelectMenu('#center_user_update',CENTERDEAFAULTOPIONT);
		clearDropDownSelectMenu('#municipal_user_update',MUNICIPALDEFAULTOPTION);
		return;
	 }else{
		 clearDropDownSelectMenu('#center_user_update',CENTERDEAFAULTOPIONT);
		 addOptionByAjax('../admin/ajaxGetMunicipalList.action',selectedProvincialCompany,'#municipal_user_update',MUNICIPALDEFAULTOPTION);
	 }   
}

function getCenterUpdateUser(){
	 var selectedMunicipalCompany=$("#municipal_user_update").val();	 
	 if(selectedMunicipalCompany==0||selectedMunicipalCompany==-1){
	   clearDropDownSelectMenu('#center_user_update',CENTERDEAFAULTOPIONT);
	   return;
}else{ 
	  addOptionByAjax('../admin/ajaxGetClaimCenter.action',selectedMunicipalCompany,'#center_user_update',CENTERDEAFAULTOPIONT);
	} 
}

function update_checkResin(id){
	if(id==REINSPECTOR_ID){
		if($("#"+id).attr("checked")){
			$("#rein_level_update").css("display","");
		}else{
			$("#rein_level_update").css("display","none");
		}
	}
	
	
	var companyRadioButtonValue=$('input[name=companyRadio_update][checked]').val();
	if(companyRadioButtonValue==CLAIM_CENTER_LEVEL){	
		checkBoxCheck('#update_role_1','#update_role_2','#update_role_3','#update_role_4','#update_role_5','#update_role_6');
	}
}

function updateUser(userId,account){
	
	var companyId;
	var roleIds="";
	var reinLevs="";
	//companyId	
	var radioValue = $('input[name=companyRadio_update][checked]').val();
	if(radioValue==1){
		var menuLayOut=$("#menuLayOut").val();
		var menuObj=evalJSON(menuLayOut);
		companyId=menuObj.userCompanyId;
	}else if(radioValue==2){
		companyId=$("#provincial_user_update").val();
	}else if(radioValue==3){
		companyId=$("#municipal_user_update").val();
	}else if(radioValue==4){
		companyId=$("#center_user_update").val();
	}
	
	//roleIds
	var checkbox=$('input[name=roleName_update][checked]');	
	var appraiserValue=$("#"+APPRAISER_ID).val();
	var reinspectorValue=$("#"+REINSPECTOR_ID).val();
	$.each(checkbox,function(i){		
		var roleId=checkbox.eq(i).val();		
		//filt by role radio button
		if(radioValue==1){
			if(roleId!=appraiserValue&&roleId!=reinspectorValue){
				roleIds+=roleId;
				roleIds+=ROLE_ID_SEPARATOR;	
			}
		}else if(radioValue==2||radioValue==3){
			if(roleId!=appraiserValue){
				roleIds+=roleId;
				roleIds+=ROLE_ID_SEPARATOR;	
			}
		}else{			
			roleIds+=roleId;
			roleIds+=ROLE_ID_SEPARATOR;	
		}
	});
	roleIds=roleIds.substring(0, roleIds.length-1);
	//roleReinLevs
	var reinLevCheckBox=$('input[name=reinLevName_update][checked]');	
	$.each(reinLevCheckBox,function(i){
		var reinLev=reinLevCheckBox.eq(i).val();	
		reinLevs+=reinLev;
		reinLevs+=ROLE_ID_SEPARATOR;
	});
	reinLevs=reinLevs.substring(0, reinLevs.length-1);
	var companyLevel;
	//companyId	
	var radioValue = $('input[name=companyRadio_update][checked]').val();
	companyLevel=radioValue;
	sendUpdateUserRequest(userId,account,companyId,roleIds,reinLevs,companyLevel);
	
}

function sendUpdateUserRequest(userId,oldaccount,companyId,roleIds,reinLevs,companyLevel){
	var userName=$("#user_userName_update");
	var account=$("#user_account_update");
	var password=$("#user_password_update");
	var telNo=$("#user_telNo_update");
	var faxNo=$("#user_faxNo_update");
	var managerId=$("#user_managerId_update");
	var email=$("#user_email_update");
	var note=$("#user_note_update");
	
	var parms={
			"saveUserObj.userId":userId,	
			"saveUserObj.userName":userName.val(),	
			"saveUserObj.account":account.val(),
			"saveUserObj.password":password.val(),
			"saveUserObj.telNo":telNo.val(),
			"saveUserObj.faxNo":faxNo.val(),
			"saveUserObj.manager":managerId.val(),
			"saveUserObj.email":email.val(),
			"saveUserObj.companyId":companyId,
			"saveUserObj.roleIds":roleIds,
			"saveUserObj.resinLevIdStr":reinLevs,
			"saveUserObj.note":note.val()
	};
	
	//check user name
	$.ajax({
        type:"POST",
        async:false,
        url:"user/ajaxCheckUserName.action",
        data:{"account":trim(account.val())},
        success:function(data) {
		if(data>0&&trim(account.val())!=oldaccount){			
					alert("用户账号已经存在,请修改用户名称");							
			}else{				
				if(checkUserInfo(userName,account,password,telNo,faxNo,managerId,email)){				
					//showParent('#UpdateUser');
					//showTip();				
					$.ajax({
				        type:"POST",
				        async:false,
				        url:"user/ajaxUserUpdate.action",
				        data:parms,
				        success:function(data2) {						
						//	hideTip();
							if(parseInt(data2)>0){		
								setValueToHiddenForUpdatePaganiation(account.val(),userName.val(),roleIds,companyId,companyLevel);
								//forward to new page
								showAlertDialogue('修改成功',forwardToNewPage,'userExcutePagination.action');						
							}else{								
								showAlertDialogue('修改失败',forwardToNewPage,'userExcutePagination.action');
							}
				          }
				    });
				}
			}
          }
    });		
}

function setValueToHiddenForUpdatePaganiation(account,userName,roleIds,companyId,companyLevel){	
	var pageParmStr=$("#pageParm").val();
	var pageParmObj=evalJSON(pageParmStr);	
	pageParmObj.account=account;
	pageParmObj.userName=userName;
	pageParmObj.roleIds=roleIds;
	pageParmObj.specialQueryCompanyType=2;
	pageParmStr=evalJSONString(pageParmObj);
	$("#pageParm").val(pageParmStr);	
	var menuLayOutStr=$("#menuLayOut").val();
	var menuObj=evalJSON(menuLayOutStr);
	menuObj.currentSelectedCompanyId=companyId;
	menuObj.currentSelectedCompanyLevel=companyLevel;
	menuLayOutStr=evalJSONString(menuObj);
	$("#menuLayOut").val(menuLayOutStr);	
	//clear page number
	$("#_p_currentPage_id").val("");
	$("#_p_totalPage_id").val("");	
}


function forwardToNewPage(url){
	$("#form1").attr("action",url);
    $("#form1").submit();
}

function checkUserInfo(userName,account,password,telNo,faxNo,managerId,email){
	//check info
	if(checkInvalidUser(userName)&&checkInvalidAccount(account)&&checkInvalidPwd(password)&&checkTel(telNo) &&
			checkFax(faxNo) &&checkManage(managerId)&&
			checkEmail(email)){
		//check the radio button
		var radioValue = $('input[name=companyRadio_update][checked]').val();
		if(radioValue==undefined){
			alert('请选择所属公司');
			return false;
		}else{
			
			if (radioValue==1){
				
			}else if(radioValue==2){
				if($("#provincial_user_update").val()==0){
					alert('请选择省级公司');
					return false;
				}
				
			}else if(radioValue==3){			
				if($("#municipal_user_update").val()==0){
					alert('请选择市级公司');
					return false;
				}
			}else if(radioValue==4){			
				if($("#center_user_update").val()==0){
					alert('请选择理赔中心');
					return false;
				}
			}
		}
		//check role
		 var roleCheckBox= $('input[name=roleName_update][checked]');
		 var roleFlag=true;
		if (radioValue==1){
			$.each(roleCheckBox,function(i){		
				var roleId=roleCheckBox.eq(i).val();
				if(roleId!=APPRAISER_ROLE_ID&&roleId!=REINSPECTOR_ROLE_ID){
					
					roleFlag=false;
				}
			});
		}else if(radioValue==2){
			$.each(roleCheckBox,function(i){		
				var roleId=roleCheckBox.eq(i).val();
				if(roleId!=APPRAISER_ROLE_ID){
					roleFlag=false;
				}
			});
			
		}else if(radioValue==3){			
			$.each(roleCheckBox,function(i){		
				var roleId=roleCheckBox.eq(i).val();
				if(roleId!=APPRAISER_ROLE_ID){
					roleFlag=false;
				}
			});
		}else if(radioValue==4){			
			$.each(roleCheckBox,function(i){		
				roleFlag=false;
			});
		}
		
		if(roleFlag==true){
			alert('请选择角色');
			return false;
		}
		
		//check reinlev
		
		var reinflag=true;
		if (radioValue==2||radioValue==3||radioValue==4){		
			if($("#"+REINSPECTOR_ID).attr("checked")){		
				 var reinLevCheckBox= $('input[name=reinLevName_update][checked]');
				 $.each(reinLevCheckBox,function(i){				
					 reinflag=false;
				});
				 if(reinflag==true){
						alert('请选择核损级别');
						return false;
					}else{
						return true;
					}
			}else{
				return true;
			}
			
		}else{
			return true;
		}
	}	
}

function checkInputData(pig) {		
	var val = pig.value;
	var patrn = new   RegExp("[\|~`!@_,./?=';:[#\$%\^&\*\<\>\{\}()\+\-]+");
	if(!val) {
        return true;
	}
	if (val.match(patrn) == null) {
		return true;
	}  else {
	   pig.value=pig.value.substr(0,pig.value.length-1);
	}	
}

function checkInvalidUser(obj){
	if(obj.val().length==0){
		alert('用户名没有填写');
		obj.focus();
		return false;
	}
	
	/*var reg = /^([a-zA-Z0-9]){0,20}$/;
	if(!reg.exec(obj.val())){
		alert("用户名输入字符非法，只能输入20个英文字母或数字，请重新输入。");
		return false;
	}*/
	return true;
}

function checkInvalidAccount(obj){
	/*if(obj.val().length==0){
		alert('用户账号没有填写');
		obj.focus();
		return false;
	}
	
	var reg = /^([a-zA-Z0-9]){0,20}$/;
	if(!reg.exec(obj.val())){
		alert("用户账号输入字符非法，只能输入20个英文字母或数字，请重新输入。");
		return false;
	}
	return true;*/
	return true;
}

function checkInvalidChar(obj){
	var reg = /^[a-zA-Z0-9]*$/;
	if(!reg.exec(obj.val())){
		alert("输入字符非法，只能输入英文字母和数字，请重新输入。");
		return false;
	}
	return true;
}

function checkInvalidPwd(obj){
	if(!isPasswordValid(obj.val())){
		alert("输入密码包含非法字符，请重新输入。");
		obj.focus();
		return false;
	}
	if(obj.val().length > 20){
		alert("密码最多20个字符，请重新输入。");
		obj.focus();
		return false;
	}
	if(obj.val().length < 8){
		alert("密码不能少于8个字符，请重新输入。");
		obj.focus();
		return false;
	}
	if(obj.val().length < 1){
		alert("请输入密码。");
		obj.focus();
		return false;
	}
	var r1 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{0,9}/;
	if(!r1.test(obj.val())){
		alert("密码必须包含大小写字母和数字");
		obj.focus();
		return false;
	}
	return true;
}

function isPasswordValid(val){
    var patrn = new   RegExp("[~`\^\*\<\>;\'\"]+");
	 if(!val) {
	     return true;
	 }
	 if (val.match(patrn) == null) {
	     return true;
	 }  else 
    {
	     return false;
	 }
}

function checkTel(obj){
	if(obj.val().length > 50){
		alert("电话号码长度不能超过50个字符");
		obj.focus();
		return false;
	}

	var patrn = /[^\+|\-|\(|\)|\d| ]/;
	if (obj.val().length > 0 && obj.val().match(patrn)) {
	    alert("电话号码格式不正确");
	    obj.focus();
	    return false;
	} 
	return true;
}

function checkFax(obj){
	if(obj.val().length > 50){
		alert("传真号码长度不能超过50个字符");
		obj.focus();
		return false;
	}
	var patrn = /[^\+|\-|\(|\)|\d| ]/;  
	if (obj.val().length > 0 && obj.val().match(patrn)) {
	    alert("传真号码格式不正确");
	    obj.focus();
	    return false;
	} 
	return true;
}

function checkManage(obj){	
	if(obj.val().length>50){
		alert('经理不能超过50个字符');
		obj.focus();
		return false;
	}
	return true;
}

function checkEmail(obj){
	if(obj.val().length > 50){
		alert("电子邮箱长度不能超过50个字符");
		obj.focus();
		return false;
	}
	var patrn = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
	if (obj.val().length > 0 && !patrn.exec(obj.val())) {
	    alert("电子邮箱格式不正确");
	    obj.focus();
	    return false;
	} 
	return true;	
}


